package com.robot.mvvm.ui.main

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.configs.AppConstants
import com.robot.base.model.ImageTextBean
import com.robot.base.util.arouter.ARouterUtil
import com.robot.mvvm.R
import com.robot.mvvm.databinding.AppFragmentWorkBenchBinding
import com.robot.mvvm.ui.main.adapter.ImageTextAdapter
import com.robot.mvvm.vm.main.WorkBenchFragmentViewModel
import me.goldze.mvvmhabit.BR

class WorkBenchFragment :
    GLBaseFragment<AppFragmentWorkBenchBinding, WorkBenchFragmentViewModel>() {

    var serveImageTextAdapter: ImageTextAdapter? = null
    var deviceImageTextAdapter: ImageTextAdapter? = null
    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_work_bench

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {

        initAdapter()
        viewModel.initData()
    }

    private fun initAdapter() {
        serveImageTextAdapter = ImageTextAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                when (position) {
                    0 -> {
                        ARouterUtil.goToActivity(AppConstants.Router.Main.A_REPAIR_WORK_ORDER)
                    }
                    11 -> {
                        //证书查看
                        ARouterUtil.goToActivity(AppConstants.Router.Main.A_SAFETY)
                    }
                }
            }
        }

        binding.mRvServe.apply {
            layoutManager =
                GridLayoutManager(requireContext(), 4, GridLayoutManager.VERTICAL, false)
            adapter = serveImageTextAdapter
        }
        deviceImageTextAdapter = ImageTextAdapter().apply {
            setOnItemClickListener { adapter, view, position ->

            }
        }
        binding.mRvDevice.apply {
            layoutManager =
                GridLayoutManager(requireContext(), 4, GridLayoutManager.VERTICAL, false)
            adapter = deviceImageTextAdapter
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.serveDataEvent.observe(this) {
            serveImageTextAdapter?.setNewInstance(it as MutableList<ImageTextBean>?)
        }

        viewModel.uc.deviceDataEvent.observe(this) {
            deviceImageTextAdapter?.setNewInstance(it as MutableList<ImageTextBean>?)
        }
    }
}